package com.probiz.estoresa.system.web.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.common.model.system.Tax;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.system.service.TaxManager;


public class TaxController extends GenericStoreAdminController<Tax> {
    private TaxManager taxManager = null;
    
    public void setTaxManager(TaxManager taxManager) {
        this.taxManager = taxManager;
    }
   
	
    /**
     * records delete
     * @param request
     * @param response
     */
    public ModelAndView multiDelete(HttpServletRequest request,
            HttpServletResponse response) {
    	if (logger.isDebugEnabled()) {
            logger.debug("entering 'TaxController multiDelete' method...");
        }
        
        //get pkey array from request 
        String[] pkeys = request.getParameterValues("multiIds");
        if (pkeys != null) {
        	boolean isDeleteSuccess=true;
        	StringBuffer sbNames=new StringBuffer();
            for (int i = 0; i < pkeys.length; i++) {
                if (pkeys[i]!=null && !pkeys[i].equals("")) {
                    Tax tax = taxManager.getById(new Integer(pkeys[i]));
                    isDeleteSuccess=true;
                	try{
                		taxManager.deleteById(new Integer(pkeys[i]));
                    }catch(Throwable t){
                    	isDeleteSuccess=false;
                    	//t.printStackTrace();
                    }
                    if (logger.isDebugEnabled()) {
                        logger.debug("TaxController delete key =" + pkeys[i]);
                    }
                    sbNames.append(",").append(tax.getTaxName());
                    
                }
            }
            if(sbNames.length()>0){
            	sbNames.delete(0, 1);
            	String message ="";
                if(isDeleteSuccess){//save message
                	message= getMessage("tax.deleted",new Object[] {sbNames}); 
                	saveMessage(request, message);
                }else{
                	 message = getMessage("tax.deleteFail",new Object[] {sbNames}); 
                	 saveErrorMessage(request, message);
                }
                
            }
        }
		return this.returnToSearch(request, response);
    }    
    
    
    /**
     * records save
     * @param request
     * @param response
     */
    public ModelAndView multiSave(HttpServletRequest request,
            HttpServletResponse response) {
        return null;
    }
    
    
	@Override
	protected String getEntityName(Tax entity) {
		return entity.getTaxName();
	}


	@Override
	protected void initController() throws Exception {
		this.mgr = taxManager;
	}

	@Override
	protected Map<String,String> getSortMap(){
		Map<String,String> sortMap = new HashMap<String,String>();
		sortMap.put("1", "convert_gbk(taxName)");
		sortMap.put("2", "taxRegisterNo");
		return sortMap;
	}
	
	@Override
	protected void preSave(HttpServletRequest request, Tax entity, BindException errors) {
		
	}
        
}
